package com.shux.usermanager.biz.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.shux.common.exception.ShuxRunTimeException;
import com.shux.redis.biz.IRedisBiz;
import com.shux.usermanager.biz.ISysRoleBiz;
import com.shux.usermanager.dao.ISysRoleDao;
import com.shux.usermanager.entity.SysRole;
import com.shux.utils.page.PageData;

/**
 **********************************************
 *  描述：
 * Simba.Hua
 * 2017年6月18日
 **********************************************
**/
@Service("sysRoleBiz")
public class SysRoleBizImpl implements ISysRoleBiz {
    private Logger logger = LoggerFactory.getLogger(getClass());
    private @Autowired ISysRoleDao sysRoleDao;
    
   // @Autowired
    @Reference(interfaceName="com.shux.redis.biz.IRedisBiz")
    private  IRedisBiz<List<SysRole>> redisBiz;
    @Override
    public SysRole loadSysRole(String roleId) {
        try {
            return sysRoleDao.loadSysRole(roleId);
        } catch (Exception e) {
            logger.error("通过ID加载角色失败",e);
            throw new ShuxRunTimeException("通过ID加载角色失败",e);
        }
    }
    @Override
    public List<SysRole> queryAllRootRole() {
        // TODO Auto-generated method stub
        PageData pageData = new PageData();
        pageData.put("parentId", 0);
        try {
            
            List<SysRole> list = redisBiz.get("sysRoles");//先从redis中取数
            if(list == null){
                list = sysRoleDao.queryAllRolesByParentId(pageData);
                redisBiz.set("sysRoles", list);
            }
            return list;
        } catch (Exception e) {
            logger.error("查询角色组失败！",e);
            throw new ShuxRunTimeException("查询角色组失败！",e);
        }
    }
    @Override
    public List<SysRole> querySysRoleByParentId(String parentRoleId) {
        // TODO Auto-generated method stub
        PageData pageData = new PageData();
        pageData.put("parentId", parentRoleId);
        try {
            List<SysRole> list = redisBiz.get("queryAllRolesByParentId:"+parentRoleId);
            if( list == null){
                list = sysRoleDao.queryAllRolesByParentId(pageData);
                redisBiz.set("queryAllRolesByParentId:"+parentRoleId, list);
            }
            return sysRoleDao.queryAllRolesByParentId(pageData);
        } catch (Exception e) {
            logger.error("通过父角色ID"+parentRoleId+"查询角色组失败！",e);
            throw new ShuxRunTimeException("通过父角色ID"+parentRoleId+"查询角色组失败！",e);
        }
    }

}
